<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   this($)*/

!defined('DYHB_PATH') && exit;

class TemplateGtkGladeWidgetCompiler_Custom extends TemplateGtkGladeWidgetCompilerBase{

	protected function compileCreateWidget_(TemplateGtkGladeWidget $oObject){
		$sWidgetClass=$oObject->getWidgetClass();
		$sWidgetId=$oObject->getWidgetId();
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oObject);
		$oObject->setWidgetId($sWidgetId);
		if(preg_match('/^@(View|TemplateController):(.+)$/i',$sWidgetId,$arrRes)){
			$sType=$arrRes[1];
			$sName=$arrRes[2];
			if($sType==='View'){// 引用 子视图
				$sCompiled="
		// 嵌入 子视图
		\$oSubView=View::getViewByGlobalName('{$sName}');
		if(\$oSubView){
			{$sVariableName}=\$oSubView->getWidget();
			if(!{$sVariableName}){
				{$sVariableName}=\$oSubView->create();
			}
		}
		else{
			{$sVariableName}=new GtkLabel(G::L('Glade 模版错误：名为“%s”的视图不存在。','Dyhb',null,'{$sName}'));
		}
";
		}
			// 非 Gtk 原生控件
			else if($sType==='TemplateController'){
				$sCompiled="
		{$sVariableName}=new GtkHBox();
		{$sVariableName}->set_name('{$sName}');
";
			}

			// 意外类型
			else
				A::ASSERT_(0);
		}// 根据 Create Function 创建窗体
		else{}
		return $sCompiled;
	}

	protected function compileChildWidgetPacking_(TemplateGtkGladeWidget $oParentWidget,TemplateGtkGladeWidget $oChildWidget,TemplateGtkGladeWidgetPacking $oPacking){
		$sParentVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oParentWidget);// 父/子 窗体变量名
		$sChildWidgetVariableName='$'.TemplateGtkGlade::getWidgetVariableName($oChildWidget);
		$sChildWidgetName=$oChildWidget->getWidgetId();// 子窗体名
		if(($sLeft=$oPacking->getProperty('left_attach'))===null){$sLeft='-1';}// 上下左右
		if(($sRight=$oPacking->getProperty('right_attach'))===null){$sRight='-1';}
		if(($sTop=$oPacking->getProperty('top_attach'))===null){$sTop='-1';}
		if(($sBottom=$oPacking->getProperty('bottom_attach'))===null){$sBottom='-1';}
		$sCompiled="\t\t{$sChildWidgetVariableName}=self::getWidget('{$sChildWidgetName}',\$arrObjectForSignal);";
		if($sLeft<0 or $sTop<0){$sCompiled.= "\t\t{$sParentVariableName}->append({$sChildWidgetVariableName});";}
		else{$sCompiled.= "\t\t{$sParentVariableName}->attach({$sChildWidgetVariableName},{$sLeft},{$sRight},{$sTop},{$sBottom});";}
		return $sCompiled;
	}

	static public function regToProcessorManager(ITemplateObjProcessorManager $oManager){
		$oManager->regProcessor(str_replace('TemplateGtkGladeWidgetCompiler_','',__CLASS__),__CLASS__);
	}

}
